﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TPCN.DAL;
using System.Data;

namespace TPCN.Admincp
{
    public partial class Doanhthutheokhachhang : System.Web.UI.Page
    {
        private static string customerID;
        private static DateTime startDate, endDate;
        private static DataTable source;
        private static double sum = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
                LoadCustomer();
        }

        protected void BindGridView()
        {
            sum = 0;
            source = new DataTable();
            source = HoaDonBanDAL.GetByDateAndCustomer(startDate, endDate, customerID);
            DoanhthuGridView.DataSource = source;
            for (int i = 0; i < source.Rows.Count; i++)
            {
                sum += Convert.ToDouble(source.Rows[i][1]);
            }
            lblMoney.Text = sum.ToString("N0");
            DoanhthuGridView.DataBind();
        }

        protected string GetProductName(Object id)
        {
            string mID = Convert.ToString(id);
            return HangDAL.GetByID(mID).Tenhang;
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            startDate = dateStart.SelectedDate.Value;
            endDate = dateEnd.SelectedDate.Value;
            customerID = cboCustomer.SelectedValue;
            BindGridView();
        }

        protected void DoanhthuGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            DoanhthuGridView.PageIndex = e.NewPageIndex;
            DoanhthuGridView.DataBind();
        }

        protected void LoadCustomer()
        {
            List<KhachHangObj> list = KhachHangDAL.GetByAll();
            cboCustomer.Items.Add(new ListItem("Tất cả", "-1"));
            cboCustomer.Items.Add(new ListItem("Khách lẻ", "0"));
            foreach (KhachHangObj p in list)
            {
                cboCustomer.Items.Add(new ListItem(p.Hovaten, p.KhachID));
            }
        }

        protected string FormatMoney(Object m)
        {
            double result = Convert.ToDouble(m);
            return result.ToString("N0");
        }

        protected string GetCustomerNameById(Object id)
        {
            string mID = Convert.ToString(id);
            KhachHangObj customer = KhachHangDAL.GetByID(mID);
            return (String.IsNullOrEmpty(customer.Hovaten)) ? "Khách lẻ" : customer.Hovaten;
        }
    }
}